package com.controller;

import com.alibaba.fastjson.JSON;
import com.entity.AlertCode;
import com.service.UserInfoMybatisServiceImpl;
import com.util.MyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;


@WebServlet({"/login"})
public class Login
        extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session;
        Map map = MyUtil.parameterNamesUtil(req, resp);

        UserInfoMybatisServiceImpl userInfoMybatisServiceImpl = new UserInfoMybatisServiceImpl();
        AlertCode alertCode = null;
        switch ((new StringBuilder()).append(map.get("mothed")).append("").toString()) {
            case "login":
                alertCode = userInfoMybatisServiceImpl.login(map);
                session = req.getSession();
                session.setAttribute("user", alertCode.getSessions());
                break;

            case "pass":
                alertCode = userInfoMybatisServiceImpl.pass(map);
                break;

            case "outLogin":
                session = req.getSession();
                session.invalidate();
                break;
        }


        String str = JSON.toJSONString(alertCode);
        resp.getWriter().println(str);
    }
}
